package cn.sdormitory.controller;

import cn.hutool.core.util.StrUtil;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.common.utils.JwtTokenUtil;
import cn.sdormitory.service.AppStudentService;
import cn.sdormitory.service.AppUserService;
import cn.sdormitory.sys.service.OnlineUserService;
import cn.sdormitory.sys.vo.LoginInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;

/**
 * @创建人：zhouyang
 * @创建时间：2020/12/25 10:46
 * @version：V1.0
 */
@RestController
@RequestMapping("/app/User")
@Api("APP端登录接口")
public class AppLoginController {

    @Resource
    private AppUserService appUserService;

    @Resource
    AppStudentService appStudentService;

    @Resource
    private OnlineUserService onlineUserService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation(value = "小程序openId一键登录")
    @GetMapping("/loginByOpenId")
    public ResultMsg loginByOpenId(@RequestParam("loginCode") String loginCode,
                                  HttpServletRequest request) {
        return appUserService.loginByOpenId(loginCode,request);
    }
    
    @ApiOperation(value = "根据Id获取登录用户信息")
    @GetMapping("/getUserInfoById")
    public ResultMsg getUserInfoById(@RequestParam("loginId") String loginId,
                                   HttpServletRequest request) {
        return appUserService.getUserInfoById(loginId,request);
    }

    @ApiOperation(value = "微信手机号一键登录")
    @GetMapping("/loginByPhone")
    public ResultMsg loginByPhone(@RequestParam("loginCode") String loginCode,
                                  @RequestParam("phoneCode") String phoneCode,
                                  HttpServletRequest request) {
        return appUserService.loginByPhone(loginCode,phoneCode,request);
    }

    @ApiOperation(value = "登录")
    @PostMapping("/doLogin")
    public ResultMsg doLogin(@RequestBody Map<String,String> map, HttpServletRequest request){
        String loginName = map.get("loginName");

        System.out.println(loginName + "loginController-doLogin()");

        String token = null;

        //判断登录用户类型（学生，家长，老师）
        int flag = -1;

        //学生登录
        token = appStudentService.login(loginName, request);

        if(StrUtil.isEmpty(token)){
            token = appUserService.doLogin(loginName, request);
            flag = 0;
        }else {
            flag = 1;
        }

//        String token = appUserService.doLogin(loginName,loginPassword,request);
        if(null != token && !"".equals(token)){
            LoginInfoVo loginInfoVo = new LoginInfoVo();
            if(flag == 0){
                loginInfoVo = appUserService.getLoginInfo(loginName);
                loginInfoVo.setToken(token);
            }
            if(flag == 1){
                loginInfoVo = appStudentService.getLoginInfo(loginName);
                loginInfoVo.setToken(token);
            }

            return ResultMsg.BY_SUCCESS("成功",loginInfoVo);
        }else{
            return ResultMsg.BY_FAIL("失败");
        }
    }

    @ApiOperation(value = "logout => 登出功能")
    @PostMapping(value = "/logout")
    public CommonResult logout(HttpServletRequest request) throws Exception {
        onlineUserService.logout(jwtTokenUtil.getToken(request));
        SecurityContextHolder.clearContext();
        return CommonResult.success();
    }

    @ApiOperation(value = "logoutByStudent => 学生登出功能")
    @PostMapping(value = "/logoutByStudent")
    public CommonResult logoutByStudent(HttpServletRequest request) throws Exception {
        onlineUserService.logAppStudentOut(jwtTokenUtil.getToken(request));
        SecurityContextHolder.clearContext();
        return CommonResult.success();
    }

}
